package reduce.fz.com.reduce.fragment;


import android.app.Service;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.adapter.BaseListAdapter;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.TargetSet;
import reduce.fz.com.reduce.bean.TeamDynamic;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.service.TeamService;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ServiceBuilder;

public class TargetFollowFragment extends BaseListFragment<TargetFollowFragment.TargetFollowViewHolder, User> {
    private TargetSet targetSet;
    private List<Long> uids=new ArrayList<Long>();
    private List<User> users=new ArrayList<User>();

    public List<Long> getFollowUids() {
        return uids;
    }
    public List<User> getFollowUsers()
    {
      return users;
    }


    public interface FollowListener {
        public void follow(User user);

        public void cancel(User user);
    }


    public static TargetFollowFragment create(TargetSet set) {
        TargetFollowFragment fragment = new TargetFollowFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("targetset", set);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        targetSet= (TargetSet) getArguments().getSerializable("targetset");
    }

    public static class TargetFollowViewHolder extends BaseRecycleViewHolder<User> implements View.OnClickListener {
        private RoundNetworkImageView muserAvatar;
        private TextView mnickname;
        private ToggleButton mFollowBtn;
        private TextView mFollowDegree;
        private FollowListener listener;
        private User user;

        public TargetFollowViewHolder(View itemView, FollowListener listener) {
            this(itemView);
            this.listener = listener;

        }


        public TargetFollowViewHolder(View itemView) {
            super(itemView);
            muserAvatar = (RoundNetworkImageView) itemView.findViewById(R.id.iv_user_avatar);
            mnickname = (TextView) itemView.findViewById(R.id.tv_user_nickname);
            mFollowBtn = (ToggleButton) itemView.findViewById(R.id.tb_follow);
            mFollowDegree = (TextView) itemView.findViewById(R.id.tv_follow_degree);

        }

        @Override
        public void renderView(User data, int position) {
            this.user=data;
            muserAvatar.setImageUrl(data.avatar, HttpUtils.getAvatarImageLoader());
            mnickname.setText(data.nickname);
            mFollowBtn.setOnClickListener(this);
            mFollowDegree.setText(data.userAttach.followDegree + "");
        }
        public void follow()
        {
            if(mFollowBtn.isChecked())
            {
               listener.cancel(user);
            }else{
                listener.follow(user);
            }

        }


        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tb_follow:
                    follow();
                    break;
                default:
                    break;
            }

        }
    }


    public TargetFollowFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        //默认单排流
        return null;
    }

    //请求URL
    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<User>>(new TypeReference<Result<List<User>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();
            }

            @Override
            public void responseInfo(Result<List<User>> data) {
                addResult(data);
            }
        });
    }

    @Override
    protected TargetFollowViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_target_recommend_user, parent, false);
        TargetFollowViewHolder viewHolder = new TargetFollowViewHolder(view, new FollowListener() {
            @Override
            public void follow(User user) {
                uids.add(user.uid);
                users.add(user);
            }

            @Override
            public void cancel(User user) {
                uids.remove(user.uid);
                users.remove(user);
            }
        });
        return viewHolder;
    }

    @Override
    protected TargetFollowViewHolder createfootViewHolder(View view) {
        return new TargetFollowViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<User> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_target_recommend_users, targetSet.teamid, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return 0;
    }


}
